/**
 * Created by woziji00226 on 2024-12-25 09:58:05.
 * 杯子有属性，存不存在硬币，还有打开杯子
 */
class Sprite_Cup_W26 extends GUI_15002 {
    //定义一个字符串，这个字符串等会用来触发播放动画结束的回调函数
    static EVENT_PLAY_OPEN_ANIMATION_COMPLETED = 'EVENT_PLAY_OPEN_ANIMATION_COMPLETED';
    //点击的时候触发
    static EVENT_OPEN = "EVENT_OPEN";

    /**
     * 存在硬币
     * private 类型让外部无法直接访问它，通过 getset 方法，暴露出去 getset方法里可以携带逻辑
     */
    private _isExistCoin: boolean = false;

    /**
     * 用于cup的缓动类对象
     */
    private _tweenCup: Tween;

    /**
     * 用于 cup倒影的缓动类对象
     */
    private _tweenCupInvertedImage: Tween;

    /**
     * 记录 原始的Cup的Y值
     */
    private _oriCupY: number;
    /**
     * 记录 原始的cup倒影的Y值。因为缓动变化以后，要恢复原值
     */
    private _oriCupIY: number;

    /**
     * 抬起的高度
     */
    private _height1: number = 50;

    /**
     * 抬起的时间 (ms)
     */
    private _time: number = 400;

    /**
     * 等待时间 ms
     */
    private _waitTime: number = 800;

    /**
     * 正在播放动画，
     */
    private _isPlaying: boolean = false;


    constructor() {
        super();
        //初始化的时候赋值false 出发set方法，将coin控件隐藏起来
        this.isExistCoin = false;

        this._tweenCup = new Tween();
        this._tweenCupInvertedImage = new Tween();

        this._oriCupY = this.cup.y;
        this._oriCupIY = this.cupInvertedImage.y;


        //自身监听一个销毁事件，当自身消耗时，清理缓动类对象
        this.once(GameSprite.ON_DISPOSE, this, () => {
            this._tweenCup.clear();
            this._tweenCupInvertedImage.clear();
            this._tweenCup = null;
            this._tweenCupInvertedImage = null;
        });


        //监听杯子 被鼠标click的事件
        this.cup.on(EventObject.CLICK, this, this.onClick);

    }

    private onClick() {
        //debugger;
        this.open();
    }

    /**
     * 打开杯子
     * 播放一个打开杯子的动画，该动画是杯子向上移动，倒影向下移动，显露出coin
     */
    public open() {
        if (this._isPlaying) {
            //如果正在播放动画，就不准再播放了
            return;
        }
        this.event(Sprite_Cup_W26.EVENT_OPEN);
        this._isPlaying = true;
        this.playOpenAnimation();
    }

    /**
     * 播放杯子动画
     * 动画使用tween类，进行
     */
    private playOpenAnimation() {
        var targetY = this.cup.y - this._height1;
        var targetY2 = this.cupInvertedImage.y + this._height1;

        //杯子y 在经过 800ms的时间以后，y值改变为 当前y值向上移动100px的值，变化曲线为quartIn
        this._tweenCup.to(this.cup, { y: targetY }, this._time, Ease.sineIn, Callback.New(() => {
            //播放结束以后等待一定时间就合上
            Callback.New(() => {
                //强制完成
                this._tweenCup.complete();
                this._tweenCupInvertedImage.complete();
                //强制刷新位置
                this.cup.y = targetY;
                this.cupInvertedImage.y = targetY2;
                //播放关闭动画
                this.playCloseAnimation();
            }, this).delayRun(this._waitTime, setTimeout);
        }, this));
        this._tweenCupInvertedImage.to(this.cupInvertedImage, { y: targetY2 }, this._time, Ease.sineIn);
    }


    /**
     * 播放把杯子合起来的动画
     */
    private playCloseAnimation() {
        this._tweenCup.to(this.cup, { y: this._oriCupY }, this._time, Ease.sineIn, Callback.New(() => {
            //强制完成
            this._tweenCup.complete();
            this._tweenCupInvertedImage.complete();
            //强制刷新位置
            this.cup.y = this._oriCupY;
            this.cupInvertedImage.y = this._oriCupIY;
            //派发完成事件
            this.event(Sprite_Cup_W26.EVENT_PLAY_OPEN_ANIMATION_COMPLETED);
            this._isPlaying = false;
        }, this));
        this._tweenCupInvertedImage.to(this.cupInvertedImage, { y: this._oriCupIY }, this._time, Ease.sineIn);
    }



    public set isExistCoin(value: boolean) {
        this._isExistCoin = value;
        //由于继承了GUI_15002类，所以可以访问这个界面里的控件,coin控件就是界面里的那个控件的名字,visible属性是可见属性,为flase的时候不可见
        this.coin.visible = this._isExistCoin;
    }

    public get isExistCoin(): boolean {
        return this._isExistCoin;
    }

}